package com.sanuo.pico.act.work;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActFlowBinding;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.Flow1;
import com.sanuo.pico.event.FlowEvent;
import com.sanuo.pico.event.LevelState;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FlowAct extends BaseWorkActivity implements SettingPage {

    ActFlowBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActFlowBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.title.setText(getResources().getStringArray(R.array.settings)[2]);
        binding.back.setOnClickListener(v -> Command.sendTouchEvent(0, 0x20));
        setTouch(binding.p1.down, () -> Command.sendTouchEvent(0, 0x7a));
        setTouch(binding.p1.up, () -> Command.sendTouchEvent(0, 0x7b));
        binding.save.setOnClickListener(v -> Command.sendTouchEvent(0, 0x43));
        binding.on.setOnClickListener(v -> Command.sendTouchEvent(0, 0x5d));
        binding.off.setOnClickListener(v -> Command.sendTouchEvent(0, 0x5e));

        onMessageEvent(Event.flow);
        onMessageEvent(Event.flow1);
        onMessageEvent(Event.level);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(FlowEvent event) {
        binding.p1.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Flow1 event) {
        binding.num1.setText(event.getValue() + "r/s");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LevelState event) {
        if (event.getValue() == 1) {
            binding.on.setSelected(true);
            binding.off.setSelected(false);
        } else if (event.getValue() == 0) {
            binding.on.setSelected(false);
            binding.off.setSelected(true);
        } else {
            binding.on.setSelected(false);
            binding.off.setSelected(false);
        }
    }
}
